﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Webcar.BLL;
using ToolsLayer;
using BusinessEntity;

namespace Webcar.Car
{
    public partial class RunRecordEdit : PageBase
    {
        protected string m_id
        {
            get { return Page.Request.QueryString["id"]; }
        }
        protected string m_flag
        {
            get { return Page.Request.QueryString["flag"]; }
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
                return;
            if (this.m_flag == "delete")
            {
                this.DeleteRunInfo();
                return;
            }
            DropDownListManager.BindDropDownDW(this.ddlDept);
            this.SetRunInfo();
        }

        private void DeleteRunInfo()
        {
            CarBLL cb = new CarBLL();
            BS_RUN_RECORDEntity r = cb.GetCarRunInfo(int.Parse(this.m_id));
            r.Delete();
            this.GotoSucessPage("运行记录已经删除。", this.GetReturnUrl());
        }

        private void SetLastPlace(string record_no)
        {
            CarBLL cb = new CarBLL();
            this.tbClassName.Text = DB.ExecuteScalar("select full_car_class_name from v_bs_car_record where record_no='" + record_no + "'");
            
            string sql = "select max(run_date_s) from v_run_record where record_no='" + record_no + "'";
            if (!string.IsNullOrEmpty(this.m_id))
                sql = "select max(run_date_s) from v_run_record where record_no='" + record_no +
                    " and run_date_s<'"+this.dtRunDate.Text+"'";
            string maxdate = DB.ExecuteScalar(sql);
            if (string.IsNullOrEmpty(maxdate))
            {
                this.lblError.Text = "该车没有上次的无功运行记录。";
                return;
            }
            sql = "select run_place from v_run_record where record_no='" + record_no +
                "' and run_date_s = '" + maxdate + "'";
            this.tbLastPlace.Text = DB.ExecuteScalar(sql);
            sql = "select run_date_s from v_run_record where record_no='" + record_no +
                            "' and run_date_s = '" + maxdate + "'";
            this.dtLastDate.Text = DB.ExecuteScalar(sql);
        }

        private void SetRunInfo()
        {
            if (string.IsNullOrEmpty(this.m_id))
                return;
            CarBLL cb = new CarBLL();
            BS_RUN_RECORDEntity r = cb.GetCarRunInfo(int.Parse(this.m_id));
            PropertyBindings pb = new PropertyBindings();
            pb.EntityObject = r;
            this.SetBindingInfo(pb);
            pb.SetBindingObjectValue();
            //得到车型和牌照号
            this.tbRecordNo.Text = DB.ExecuteScalar("select record_no from bs_car_record where car_no='" + r.car_no + "'");
            this.tbClassName.Text = DB.ExecuteScalar("select full_car_class_name from v_run_record where car_no='" + r.car_no + "'");

            //InfoChecker ic = new InfoChecker();
            //ic.SetInfoReaded(sendinfo.XH, InfoCheckType.CarSend);
        }

        private void SetBindingInfo(PropertyBindings pb)
        {
            pb.AddRelation(this.ddlDept, "dept_code");
         
            pb.AddRelation(this.tbCertifiedMan, "cetify_man");
            pb.AddRelation(this.tbDp, "dp");
            pb.AddRelation(this.tbDq, "dq");
            pb.AddRelation(this.tbDriverName, "driver_name");
            pb.AddRelation(this.tbLastPlace, "last_place");
            pb.AddRelation(this.tbQt, "qt");
            pb.AddRelation(this.tbRunPlace, "run_place");
            pb.AddRelation(this.tbSc, "sc");
            pb.AddRelation(this.tbYY, "yy");
            pb.AddRelation(this.dtLastDate, "last_date");
            pb.AddRelation(this.dtRunDate, "run_date");
        }

        private bool SaveInfo()
        {
            if (!this.CheckData())
                return false;
            BS_RUN_RECORDEntity sendinfo = new BS_RUN_RECORDEntity();
            CarBLL cb = new CarBLL();
            if (!string.IsNullOrEmpty(this.m_id))
                sendinfo = cb.GetCarRunInfo(int.Parse(this.m_id));
            
            PropertyBindings pb = new PropertyBindings();
            pb.EntityObject = sendinfo;
            this.SetBindingInfo(pb);
            pb.GetBindingObjectValue();
            //根据牌照号得到车号
            string car_no = cb.GetCarNoByRecord(this.tbRecordNo.Text);
            if (string.IsNullOrEmpty(car_no))
            {
                this.lblError.Text = "牌照号" + this.tbRecordNo.Text + "不存在，请检查。";
                return false;
            }
            sendinfo.car_no = car_no;
            sendinfo.Save();
            return true;
            //InfoChecker ic = new InfoChecker();
            //ic.SetInfoReaded(sendinfo.XH, InfoCheckType.CarSend);
        }
        #region checkdata
        protected bool CheckData()
        {
            this.lblError.Text = "";
            string s_error = "";

            if (this.ddlDept.SelectedValue== "")
            {
                s_error = "请选择所属单位。";
                this.lblError.Text = s_error;
                return false;
            }
            if (this.dtRunDate.Text == "")
            {
                s_error = "请输入运行时间。";
                this.lblError.Text = s_error;
                return false;
            }
            if (!PublicFunction.IsDate(this.dtRunDate.Text))
            {
                s_error = "运行时间格式有误。";
                this.lblError.Text = s_error;
                return false;
            }
            if (this.dtLastDate.Text != "")
            {
                if (!PublicFunction.IsDate(this.dtLastDate.Text))
                {
                    s_error = "上次运行时间格式有误。";
                    this.lblError.Text = s_error;
                    return false;
                }
            }

            if (this.tbRecordNo.Text == "")
            {
                s_error = "请输入牌照号。";
                this.lblError.Text = s_error;
                return false;
            }
            if (this.tbDriverName.Text == "")
            {
                s_error = "请输入驾驶员。";
                this.lblError.Text = s_error;
                return false;
            }
            if (this.tbRunPlace.Text == "")
            {
                s_error = "请输入运行地点。";
                this.lblError.Text = s_error;
                return false;
            }
           

            return true;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if(this.SaveInfo())
                this.GotoSucessPage("车辆运行记录已经保存。", this.GetReturnUrl());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetLastPlace(this.tbRecordNo.Text.Trim());
            }
            catch
            { }
        }
    }
}
